package com.pan.controller;

import com.pan.annotation.LoginRequired;
import com.pan.entity.Result;
import com.pan.service.CartService;
import com.pan.vo.CartVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Resource
    private CartService cartService;

    @GetMapping("/getAll")
    @ResponseBody
    @LoginRequired
    public Result getAllCart(@CookieValue("userTicket") String userTicket){
        Result result = cartService.getAllCartByUser(userTicket);
        return result;
    }

    @PostMapping("/add")
    @ResponseBody
    @LoginRequired
    public Result addCart(@CookieValue("userTicket") String userTicket,
                          @RequestBody List<CartVo> cartList){
        Result result = cartService.addCart(userTicket, cartList);
        return result;
    }

    @PostMapping("/update")
    @ResponseBody
    @LoginRequired
    public Result cartModifyQuantity(@CookieValue("userTicket") String userTicket,
                                   @RequestParam("goodsId")Long goodsId,
                                   @RequestParam("modifyQuantity")Integer modifyQuantity){
        Result result = cartService.updateGoodsCount(userTicket, goodsId, modifyQuantity);
        return result;
    }

    @PostMapping("/delete")
    @LoginRequired
    @ResponseBody
    public Result deleteCart(@CookieValue("userTicket") String userTicket,
                             @RequestParam("goodsId") Long goodsId){
        Result result = cartService.deleteGoods(userTicket,goodsId);
        return result;
    }



}
